package com.czjtxy.extraTerm.serverImpl;

import com.czjtxy.extraTerm.dao.TuserDao;
import com.czjtxy.extraTerm.daoImpl.TuserDaoImpl;
import com.czjtxy.extraTerm.entity.DO.Tuser;
import com.czjtxy.extraTerm.server.UserService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: staff-house
 * @author: MingYi
 * @create: 2021/06/30 08:11
 */
public class UserServiceImpl implements UserService {
    TuserDao tuserDao = new TuserDaoImpl();

    @Override
    public Tuser login(String loginName, String pwd) throws SQLException, ClassNotFoundException {
        Tuser tuser = null;
        try {
            tuser = tuserDao.login(loginName, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tuserDao.closeResource();
        }
        return tuser;
    }

    @Override
    public List<Tuser> getAll() throws SQLException, ClassNotFoundException {
        List<Tuser> tusers = null;
        try {
            tusers = tuserDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            关闭数据库
            tuserDao.closeResource();
        }
        return tusers;
    }

    @Override
    public List<Tuser> getPart(Tuser tuser) throws SQLException, ClassNotFoundException {
        List<Tuser> tusers = new ArrayList<>();
        try {
            tusers = tuserDao.getPart(tuser);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tuserDao.closeResource();
        }
        return tusers;
    }

    @Override
    public Tuser getOne(int uid) throws SQLException {
        Tuser tuser = null;
        try {
            tuser = tuserDao.getOne(uid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tuserDao.closeResource();
        }
        return tuser;
    }

    @Override
    public void updateUser(Tuser tuser) throws SQLException {
        try {
            tuserDao.updateUser(tuser);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tuserDao.closeResource();
        }
    }

    @Override
    public void deleteUser(String ids) throws SQLException {
        try {
            String[] is = ids.split(",");
            for (String s : is) {
                int uid = Integer.parseInt(s);
                tuserDao.deleteUser(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tuserDao.closeResource();
        }
    }

    @Override
    public void addUser(Tuser tuser) throws SQLException {
        try {
            tuserDao.addUser(tuser);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tuserDao.closeResource();
        }
    }
}
